/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controllers.sysAdmin;

import controllers.utils.DatabaseHjelper;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import models.Institute;

/**
 *
 * @author dybvik
 */
@SessionScoped
@Named(value="instituteList")
public class InstituteList implements Serializable{
    private final List<Institute> institutes = new ArrayList<Institute>();
    private final DatabaseHjelper dbh = DatabaseHjelper.hent();

    /** Creates a new instance of InstituteList */
    public InstituteList() {
    }

    public String navToInstituteList(){
        updateInstitutes();
        return "instituteList";
    }

    private void updateInstitutes() {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet res = null;
        try {
            conn = dbh.finnForbindelse();
            stmt = conn.prepareStatement("select * from institute");
            res = stmt.executeQuery();
            synchronized (institutes) {
                institutes.clear();
                while (res.next()){
                    Institute inst = new Institute();
                    inst.setInstituteID(res.getInt("instituteID"));
                    inst.setName(res.getString("instituteName"));
                    inst.setAddress(res.getString("instAddress"));
                    inst.setPhoneNr(res.getString("instPhoneNr"));
                    inst.setNote(res.getString("instNote"));
                    System.out.println(inst.toString());
                    institutes.add(inst);
                }
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            DatabaseHjelper.lukk(res);
            DatabaseHjelper.lukk(stmt);
            DatabaseHjelper.lukk(conn);
        }
    }

    public void executeChanges() {
        for (Institute i : institutes) {
            if (i.isEditable()) {
                saveChanges(i);
                i.setEditable(false);
            }
        }
    }

    private void saveChanges(final Institute i) {
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = dbh.finnForbindelse();
            stmt = conn.prepareStatement("update institute set instituteName=?, instAddress=?, instPhoneNr=?, instNote=? WHERE instituteID=?");
            stmt.setString(1, i.getName());
            stmt.setString(2, i.getAddress());
            stmt.setString(3, i.getPhoneNr());
            stmt.setString(4, i.getNote());
            stmt.setInt(5, i.getInstituteID());
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("Feil i lagring av institutt: " + e);
        } finally {
            DatabaseHjelper.lukk(stmt);
            DatabaseHjelper.lukk(conn);
        }
    }
    
    public String setDeleteFlagAndNavigate(Institute i) {
        i.setDeleteFlag(true);
        return "confirmDeleteInstitute";
    }

    public String executeDelete(){
        for (Institute inst: institutes) {
            if (inst.isDeleteFlag()) {
                deleteInstitute(inst);
                institutes.remove(inst);
                break;
            }
        }
        return navToInstituteList();
    }

     private void deleteInstitute(Institute i){
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = dbh.finnForbindelse();
            stmt = conn.prepareStatement("DELETE FROM institute WHERE instituteID=?");
            stmt.setInt(1, i.getInstituteID());
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("Feil i sletting av institutt: " + e);
        } finally {
            DatabaseHjelper.lukk(stmt);
            DatabaseHjelper.lukk(conn);
        }
    }

    public String revertFlagsAndNavigate(){
        for(Institute inst: institutes){
            if(inst.isEditable()) inst.setEditable(false);
            if(inst.isDeleteFlag()) inst.setDeleteFlag(false);
        }
        return navToInstituteList();
    }

    public List<Institute> getInstitutes() {
        return institutes;
    }

}
